package com.xu.wemall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xu.wemall.entry.Order;
import com.xu.wemall.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "订单操作接口")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService iOrderService;

    @ApiOperation("分页查询数据")
    @GetMapping("/getPageList")
    public IPage<Order> getPageList() {
        Page<Order> iPage = new Page<>(1, 10);
        QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
        IPage<Order> userRecord = iOrderService.page(iPage, queryWrapper);
        userRecord.getRecords().forEach(System.out::println);
        return userRecord;
    }

    @ApiOperation("查询所有数据")
    @GetMapping("/getAllList")
    public List<Order> getAllList() {
        final int pageSize = 10;
        int pageNum = 1;

        List<Order> dataList = new ArrayList<>();
        IPage<Order> iPage = new Page<>(pageNum, pageSize);
        do {
            iPage.setCurrent(pageNum);
            iPage= iOrderService.page(iPage);
            if (CollectionUtils.isEmpty(iPage.getRecords())){
                break;
            }
            dataList.addAll(iPage.getRecords());
            pageNum++;
        } while (!CollectionUtils.isEmpty(iPage.getRecords()));
        return dataList;
    }

}
